package com.xuecheng.content.controller;

import com.xuecheng.base.exception.XueChengPlusException;
import com.xuecheng.content.model.dto.BindTeachplanMediaDto;
import com.xuecheng.content.model.dto.SaveTeachplanDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.service.TeachplanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Hes
 * @description 课程计划编辑接口
 * @date: 2023/6/20 18:55
 */
@RestController
@Api(value = "课程计划编辑接口", tags = "课程计划编辑接口")
public class TeachplanController {
    @Autowired
    TeachplanService teachplanService;

    /**
     * 获取课程计划树形结构和媒资信息
     *
     * @param courseId
     * @return
     */
    @ApiOperation("获取课程计划树形结构和媒资信息")
    @GetMapping("/teachplan/{courseId}/tree-nodes")
    public List<TeachplanDto> getTreeNodes(@PathVariable("courseId") Long courseId) {
        List<TeachplanDto> teachplanTree = teachplanService.findTeachplanTree(courseId);
        return teachplanTree;
    }

    /**
     * 添加教学计划
     *
     * @param saveTeachplanDto
     */
    @ApiOperation("添加教学计划")
    @PostMapping("/teachplan")
    public void saveTeachplan(@RequestBody SaveTeachplanDto saveTeachplanDto) {
        Long courseId = saveTeachplanDto.getCourseId();
        String pname = saveTeachplanDto.getPname();
        Long parentid = saveTeachplanDto.getParentid();
        Integer grade = saveTeachplanDto.getGrade();
        if (courseId == null || StringUtils.isEmpty(pname) || parentid == null || grade == null) {
            XueChengPlusException.cast("添加教学计划失败，教学计划名称或者其他参数为空");
        }
        teachplanService.saveTeachplan(saveTeachplanDto);
    }

    /**
     * 删除教学计划
     *
     * @param teachplanId
     */
    @ApiOperation("删除教学计划")
    @DeleteMapping("/teachplan/{teachplanId}")
    public void delTeachplan(@PathVariable("teachplanId") Long teachplanId) {
        if (teachplanId == null) {
            XueChengPlusException.cast("teachplanId不能为空");
        }
        teachplanService.delTeachplan(teachplanId);
    }

    /**
     * 课程计划列表-下移
     *
     * @param teachplanId
     */
    @ApiOperation("课程计划列表-下移")
    @PostMapping("/teachplan/movedown/{teachplanId}")
    public void moveDown(@PathVariable("teachplanId") Long teachplanId) {
        teachplanService.moveDown(teachplanId);
    }

    /**
     * 课程计划列表-上移
     *
     * @param teachplanId
     */
    @ApiOperation("课程计划列表-上移")
    @PostMapping("/teachplan/moveup/{teachplanId}")
    public void moveUp(@PathVariable("teachplanId") Long teachplanId) {
        teachplanService.moveUp(teachplanId);
    }

    /**
     * 课程计划绑定媒资
     *
     * @param teachplanMediaDto
     */
    @ApiOperation("课程计划绑定媒资")
    @PostMapping("/teachplan/association/media")
    public void associationMedia(@RequestBody(required = false) BindTeachplanMediaDto teachplanMediaDto) {
        teachplanService.associationMedia(teachplanMediaDto);
    }

    /**
     *
     * @param teachPlanId
     * @param mediaId
     */
    @ApiOperation("删除课程计划绑定媒资")
    @DeleteMapping("/teachplan/association/media/{teachPlanId}/{mediaId}")
    public void delAssociation (@PathVariable("teachPlanId") Long teachPlanId, @PathVariable("mediaId") String mediaId){
        System.out.println("进入conreoller");
        teachplanService.delAssociation(mediaId,teachPlanId);
    }
}
